package javase.jmx.monitoring;

import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.monitor.MonitorNotification;

public class MonitorNotificationListener implements NotificationListener {

	@Override
	public void handleNotification(Notification notification, Object handback) {
		if (notification instanceof MonitorNotification) {
			MonitorNotification monitorNotification = (MonitorNotification) notification;
			echo("Attribute " + monitorNotification.getObservedAttribute() + " stepped over threshold " + monitorNotification.getTrigger());
		}
	}

	@SuppressWarnings("unused")
	private void printAdditionalInfo(MonitorNotification monitorNotification) {
		echo("Message " + monitorNotification.getMessage());
		echo("Attribute " + monitorNotification.getObservedAttribute());
		echo("Message " + monitorNotification.getType());
		echo("Gauge " + monitorNotification.getDerivedGauge().toString());
		echo("ObjectName " + monitorNotification.getObservedObject().getCanonicalKeyPropertyListString());
		echo("Sequence number " + monitorNotification.getSequenceNumber());
		echo("Source " + monitorNotification.getSource().toString());
		echo("UserData " + monitorNotification.getUserData().toString());
	}

	private void echo(String message) {
		System.out.println("MonotorNotificationListener: " + message);
	}
}
